//*****************************************************************************
// 1. Års eksamen @ Datamatiker - EASJ
// Holmegaard Heldagsskole Administrations system 
// 
// Denne klasse er udarbejdet af: Marc, Kasper og Nikolaj
//*****************************************************************************
package view;

import control.ControlUtil;
import javax.swing.DefaultListModel;
import model.Elev;
import model.Hold;

public class HoldAdminPanel extends javax.swing.JPanel {

    private ControlUtil controlUtil;
    private boolean rediger;
    private DefaultListModel holdModel = new DefaultListModel();

    public HoldAdminPanel(ControlUtil controlUtil) {

        this.controlUtil = controlUtil;

        initComponents();


        nulstil();
        rediger = false;
        elevList.setEnabled(false);
        sletKnap.setVisible(false);
        redigerKnap.setEnabled(false);
        opretKnap.setEnabled(false);
        aarField.setEnabled(false);

    }

    private void opdaterCombo() {
        holdCombo.removeAllItems();
        holdCombo.setEnabled(true);
        holdCombo.addItem("Opret hold");
        Hold[] gh = controlUtil.getHoldHandler().getHold();
        for (int i = 0; i < gh.length; i++) {
            holdCombo.addItem(gh[i]);
        }
    }

    private void opdaterHold(Hold hold) {

        Elev[] elevarr = controlUtil.getHoldHandler().getElev(hold);
        holdModel.clear();
        for (int i = 0; i < elevarr.length; i++) {
            holdModel.addElement(elevarr[i]);
        }
    }

    private void nulstil() {

        opdaterCombo();
        idField.setEnabled(true);
        holdModel.clear();
        idField.setText("");
        aarField.setText("");
        holdCombo.setSelectedIndex(0);
        aarField.setEnabled(false);
        rediger = false;
        elevList.setEnabled(false);
        sletKnap.setVisible(false);
        redigerKnap.setEnabled(false);
        opretKnap.setEnabled(false);
        aarField.setEnabled(false);
        holdCombo.setEnabled(true);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        elevList = new javax.swing.JList(holdModel);
        jPanel1 = new javax.swing.JPanel();
        jLabel4 = new javax.swing.JLabel();
        idField = new javax.swing.JTextField();
        jLabel5 = new javax.swing.JLabel();
        aarField = new javax.swing.JTextField();
        sletKnap = new javax.swing.JButton();
        opretKnap = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        holdCombo = new javax.swing.JComboBox();
        redigerKnap = new javax.swing.JButton();

        setLayout(null);

        jLabel1.setText("Vælg Hold:");
        add(jLabel1);
        jLabel1.setBounds(22, 21, 100, 14);

        jLabel2.setText("Elever på holdet:");
        add(jLabel2);
        jLabel2.setBounds(20, 90, 120, 14);

        jScrollPane2.setHorizontalScrollBarPolicy(javax.swing.ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);

        elevList.setToolTipText("");
        elevList.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                elevListValueChanged(evt);
            }
        });
        jScrollPane2.setViewportView(elevList);

        add(jScrollPane2);
        jScrollPane2.setBounds(20, 110, 240, 320);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Opret nyt hold"));
        jPanel1.setLayout(null);

        jLabel4.setText("Hold ID:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(40, 70, 90, 14);

        idField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                idFieldKeyReleased(evt);
            }
        });
        jPanel1.add(idField);
        idField.setBounds(108, 63, 80, 20);

        jLabel5.setText("Oprettelsesår:");
        jPanel1.add(jLabel5);
        jLabel5.setBounds(10, 100, 90, 14);

        aarField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                aarFieldKeyReleased(evt);
            }
        });
        jPanel1.add(aarField);
        aarField.setBounds(110, 100, 80, 20);

        sletKnap.setText("Slet hold");
        sletKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                sletKnapActionPerformed(evt);
            }
        });
        jPanel1.add(sletKnap);
        sletKnap.setBounds(100, 200, 100, 20);

        opretKnap.setText("Opret hold");
        opretKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                opretKnapActionPerformed(evt);
            }
        });
        jPanel1.add(opretKnap);
        opretKnap.setBounds(90, 150, 120, 40);

        jLabel3.setText("ex: 2012");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(200, 100, 70, 14);

        add(jPanel1);
        jPanel1.setBounds(520, 110, 290, 300);

        holdCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Vælg hold" }));
        holdCombo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                holdComboActionPerformed(evt);
            }
        });
        holdCombo.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                holdComboKeyReleased(evt);
            }
        });
        add(holdCombo);
        holdCombo.setBounds(20, 40, 120, 20);

        redigerKnap.setText("Rediger hold");
        redigerKnap.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                redigerKnapActionPerformed(evt);
            }
        });
        add(redigerKnap);
        redigerKnap.setBounds(80, 440, 140, 50);
    }// </editor-fold>//GEN-END:initComponents

    private void elevListValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_elevListValueChanged
    }//GEN-LAST:event_elevListValueChanged

    private void holdComboActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_holdComboActionPerformed
        if (holdCombo.getSelectedIndex() == 0) {
            nulstil();
        } else {

            if (holdCombo.getSelectedIndex() != 0 && holdCombo.getItemCount() != 0) {
                Hold hold = (Hold) holdCombo.getSelectedItem();
                opdaterHold(hold);
                if (holdCombo.getSelectedIndex() != 0) {
                    redigerKnap.setEnabled(true);
                    idField.setText(hold.getHoldId() + "");
                    aarField.setText(hold.getAar() + "");
                    idField.setEnabled(false);
                    aarField.setEnabled(false);
                }
            }
        }
    }//GEN-LAST:event_holdComboActionPerformed

    private void sletKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_sletKnapActionPerformed
        Hold hold = (Hold) holdCombo.getSelectedItem();
        controlUtil.getHoldHandler().sletHold(hold);
        nulstil();
    }//GEN-LAST:event_sletKnapActionPerformed

    private void idFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_idFieldKeyReleased
        aarField.setEnabled(true);
    }//GEN-LAST:event_idFieldKeyReleased

    private void aarFieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_aarFieldKeyReleased
        opretKnap.setEnabled(true);
    }//GEN-LAST:event_aarFieldKeyReleased

    private void redigerKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_redigerKnapActionPerformed
        rediger = true;
        holdCombo.setEnabled(false);
        sletKnap.setVisible(true);
        opretKnap.setEnabled(true);
        opretKnap.setText("Gem hold");
        idField.setEnabled(true);
        aarField.setEnabled(true);
    }//GEN-LAST:event_redigerKnapActionPerformed

    private void opretKnapActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_opretKnapActionPerformed
        if (rediger == true) {
            Hold holdet = new Hold(idField.getText(), aarField.getText());
            controlUtil.getHoldHandler().redigerHold(holdet, (Hold) holdCombo.getSelectedItem());
        } else {
            Hold hold = new Hold(idField.getText(), aarField.getText());
            controlUtil.getHoldHandler().nytHold(hold);
        }
        nulstil();
    }//GEN-LAST:event_opretKnapActionPerformed
    private void holdComboKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_holdComboKeyReleased
    }//GEN-LAST:event_holdComboKeyReleased
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField aarField;
    private javax.swing.JList elevList;
    private javax.swing.JComboBox holdCombo;
    private javax.swing.JTextField idField;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton opretKnap;
    private javax.swing.JButton redigerKnap;
    private javax.swing.JButton sletKnap;
    // End of variables declaration//GEN-END:variables
}